

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using JTC.MiniCommerce.Core;
using JTC.MiniCommerce.Store;
using JTC.MiniCommerce.Store.Services.ShippingService;
using JTC.MiniCommerce.Store.Web.Controls;
using JTC.MiniCommerce.Localization;

namespace JTC.MiniCommerce.Web.admin.controls.configuration.shippingproviders {
  public partial class shippinggeneralsettings : ShippingConfigurationControl {

    #region Member Variables
    
    ShippingServiceSettings shippingServiceSettings;
    
    #endregion
    
    #region Page Events

    /// <summary>
    /// Handles the Load event of the Page control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
    protected void Page_Load(object sender, EventArgs e) {
      try {
        SetShippingGeneralSettingsProperties();
        shippingServiceSettings = ShippingService.FetchConfiguredShippingProviders();
        if(shippingServiceSettings == null) {
          shippingServiceSettings = new ShippingServiceSettings();
        }
        if(!Page.IsPostBack) {
          LoadCountries();
          chkUseShipping.Checked = shippingServiceSettings.UseShipping;
          txtShipFromZip.Text = shippingServiceSettings.ShipFromZip;
          ddlShipFromCountry.SelectedValue = shippingServiceSettings.ShipFromCountryCode;
          txtShippingBuffer.Text = StoreUtility.GetFormattedAmount(shippingServiceSettings.ShippingBuffer, false);
        }
      }
      catch(Exception ex) {
        Logger.Error(typeof(shippinggeneralsettings).Name + ".Page_Load", ex);
        base.MasterPage.MessageCenter.DisplayCriticalMessage(LocalizationUtility.GetCriticalMessageText(ex.Message));
      }
    }

    /// <summary>
    /// Handles the Click event of the btnSave control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
    protected void btnSave_Click(object sender, EventArgs e) {
      try {
        shippingServiceSettings.UseShipping = chkUseShipping.Checked;
        shippingServiceSettings.ShipFromZip = txtShipFromZip.Text.Trim();
        shippingServiceSettings.ShipFromCountryCode = ddlShipFromCountry.SelectedValue;
        decimal buffer = 0.00M;
        decimal.TryParse(txtShippingBuffer.Text.Trim(), out buffer);
        shippingServiceSettings.ShippingBuffer = buffer;
        int id = base.Save(shippingServiceSettings, WebUtility.GetUserName());
        if(id > 0) {
          MasterPage.MessageCenter.DisplaySuccessMessage(LocalizationUtility.GetText("lblShippingSettingsSaved"));
        }
        else {
          MasterPage.MessageCenter.DisplayFailureMessage(LocalizationUtility.GetText("lblShippingSettingsNotSaved"));
        }
      }
      catch(Exception ex) {
        Logger.Error(typeof(shippinggeneralsettings).Name + ".btnSave_Click", ex);
        base.MasterPage.MessageCenter.DisplayCriticalMessage(LocalizationUtility.GetCriticalMessageText(ex.Message));
      }     
    }
    
    #endregion
    
    #region Methods
    
    #region Private

    /// <summary>
    /// Loads the countries.
    /// </summary>
    private void LoadCountries() {
      CountryCollection countryCollection = new CountryController().FetchAll().OrderByAsc("Name");
      ddlShipFromCountry.DataSource = countryCollection;
      ddlShipFromCountry.DataValueField = "Code";
      ddlShipFromCountry.DataTextField = "Name";
      ddlShipFromCountry.DataBind();
    }

    /// <summary>
    /// Sets the shipping general settings properties.
    /// </summary>
    private void SetShippingGeneralSettingsProperties() {
      LocalizationUtility.AddPanelText(pnlGeneralSettings);
      LocalizationUtility.AddButtonText(btnSave);
      LocalizationUtility.AddLabelText(lblUseShipping);
      LocalizationUtility.AddLabelText(lblShipFromZip);
      LocalizationUtility.AddLabelText(lblShipFromCountry);
      LocalizationUtility.AddLabelText(lblShippingBuffer);
      LocalizationUtility.AddHoverHelp(hlUseShippingHelp);
      LocalizationUtility.AddHoverHelp(hlShipFromZipHelp);
      LocalizationUtility.AddHoverHelp(hlShipFromCountryHelp);
      LocalizationUtility.AddHoverHelp(hlShippingBufferHelp);
      lblShippingBufferCurrencySymbol.Text = SiteSettings.Load().CurrencySymbol;
    }
    
    #endregion
    
    #endregion
    
  }
}